package com.jilefojing.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jilefojing.beans.data.web.response.SourceData;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.beans.vo.Source;
import com.jilefojing.service.ISourceService;
import com.jilefojing.util.Detect;
import com.jilefojing.web.base.BaseWeb;
import com.jilefojing.web.convent.CommonConvent;

@Controller
@RequestMapping("search")
public class SearchWeb extends BaseWeb {
	@Autowired
	private ISourceService sourceService;

	@RequestMapping("list-part.html")
	public String getSearchResultPartPage(HttpServletRequest request,
			String keywords, Integer page) {
		if (Detect.notEmpty(keywords)) {
			int index = 1;
			if (page != null) {
				index = page;
			}
			Pagination<Source> pagination = new Pagination<Source>(index, 20,
					true);

			pagination = sourceService.getPaginationByKeywordsForActive(
					keywords, pagination, null);

			if (Detect.notEmpty(pagination.getItems())) {
				List<SourceData> sourceDatas = CommonConvent
						.convent2SourceDatas(pagination.getItems());

				request.setAttribute("sources", sourceDatas);
				request.setAttribute("pagination", pagination);
			}
		}
		return "web/search/search_list_part";
	}

	@RequestMapping("list.html")
	public String toSearchResultPage(HttpServletRequest request, String keywords) {
		if (Detect.notEmpty(keywords)) {
			Pagination<Source> pagination = new Pagination<Source>(1, 20, true);

			pagination = sourceService.getPaginationByKeywordsForActive(
					keywords, pagination, null);

			if (Detect.notEmpty(pagination.getItems())) {
				List<SourceData> sourceDatas = CommonConvent
						.convent2SourceDatas(pagination.getItems());

				request.setAttribute("sources", sourceDatas);
				request.setAttribute("pagination", pagination);
			}
		}
		return "web/search/search_list";
	}
}
